package com.bstek.pms.tools.service;

import com.bstek.pms.tools.dao.SchemaDao;
import com.bstek.pms.tools.model.DBProperties;
import com.bstek.pms.tools.model.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class SchemaService {

    @Autowired
    private SchemaDao schemaDao;

    /**
     * 查询所有模板
     * @return
     */
    public List<Schema> findAll(){
        return schemaDao.findAll();
    }

    /**
     * 通过模板名查找模板
     * @param name
     * @return
     */
    public Schema findByName(String name){
        return schemaDao.findByName(name);
    }

    /**
     * 新增模板
     * @param schema
     * @return
     */
    public int insertSchema(Schema schema){
        return schemaDao.insertSchema(schema);
    }

    /**
     * 删除模板
     * @param name
     * @return
     */
    public int deleteSchemaByName(String name) {
        return schemaDao.deleteSchemaByName(name);
    }

    /**
     * 保存模板
     * @param dbProperties
     * @param params
     */
    @Transactional
    public void saveSchema(DBProperties dbProperties, Map<String,Object> params){
        Schema schema = new Schema();
        schema.setDriver(dbProperties.getDbDriver());
        schema.setUrl(dbProperties.getDbUrl());
        schema.setUserName(dbProperties.getDbUserName());
        schema.setPassword(dbProperties.getDbPwd());
        schema.setSysName((String)params.get("sysName"));
        schema.setSysNo((String)params.get("sysNo"));
        schema.setProjectNo((String)params.get("projectNo"));
        schema.setSubSysName((String)params.get("subSysName"));
        schema.setName((String)params.get("name"));
        insertSchema(schema);
    }




}
